require "task_list/task"

class Query

  def initialize()
    @querys = Array.new
  end
  
  def add(attr, value, op)
    case op
    when "=", ">", "<"
      @querys.push [attr + " = ?", value]
    when "contain"
      @querys.push [attr + " like ?", "%" + value + "%"]
    end
  end
  
  def execute()
    query = [""]
    @querys.each do |each_query|
      if("" != query[0]) then
        query[0] += " AND "
      end
      query[0] += each_query[0]
      query.push each_query[1];
    end
    Task.find(:all, :conditions => query)
  end
end

if __FILE__ == $0
  path = File.dirname(__FILE__) + "/../"
  i = $LOAD_PATH.index(path)
  if i.nil? then 
    $LOAD_PATH.push(path)
  end
  
  require "task_list/task"
  
  query = Query.new
  query.add("title", "AAA", "contain")
  query.add("user_id", "4", "=")
  query.add("title", "1", "contain")
  tasks = query.execute
  for task in tasks do
    print task
  end
end